package com.demo.paypalmanagement.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.paypalmanagement.common.BuildQuery;
import com.demo.paypalmanagement.common.ResultMsg;
import com.demo.paypalmanagement.config.response.BizException;
import com.demo.paypalmanagement.convert.ProductsConvert;
import com.demo.paypalmanagement.model.entity.Products;
import com.demo.paypalmanagement.model.vo.ProductsVO;
import com.demo.paypalmanagement.model.vo.Result;
import com.demo.paypalmanagement.service.IProductsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import java.io.IOException;

/**
 * <p>
 * 商品表，存储系统中的商品信息 前端控制器
 * </p>
 *
 * @author dayu
 * @since 2024-12-02
 */
@RestController
@RequestMapping("/products")
public class ProductsController {

    @Autowired
    private IProductsService productsService;

    @GetMapping("/page")
    public IPage<Products> productsIPage(ProductsVO vo) {
        if (ObjectUtils.isEmpty(vo)) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        Page<Products> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        return productsService.page(page, BuildQuery.buildProductItemQuery(vo));
    }

    @GetMapping("/update")
    public Result update(ProductsVO vo) {
        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        return Result.success(productsService.updateById(ProductsConvert.INSTANCE.convertVOToEntity(vo)));
    }

    @GetMapping("/delete")
    public Result remove(ProductsVO vo) {
        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        return Result.success(productsService.removeById(vo.getId()));
    }

    @GetMapping("/get")
    public Result get(ProductsVO vo) {
        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        return Result.success(productsService.getById(vo.getId()));
    }

    @PostMapping("/saveOrUpdate")
    public Result save(ProductsVO vo) throws IOException {
        return Result.success(productsService.customSaveProduct(vo));
    }

    @GetMapping("/lottery")
    public Result get() {
        return Result.success(productsService.lottery());
    }

}
